/**
 * @filename:StkOtherInController Feb 5, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd.
 * All right reserved.
 */
package com.wonder.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.PrdFeed;
import com.wonder.app.domain.PrdReturn;
import com.wonder.app.request.PrdFeedRequest;
import com.wonder.app.request.PrdReturnRequest;
import com.wonder.app.service.PrdFeedService;
import com.wonder.app.service.PrdReturnService;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.framework.authorization.CheckAdminToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Api(tags = "生产退料单")
@RestController
@RequestMapping("/app/PrdReturn")
public class PrdReturnController {

@Autowired
private PrdReturnService prdReturnService;

    @Autowired
    private CheckAdminToken checkAdminToken;

    @Autowired
    private SysAdminService adminService;


    private Map<Long,Long> lastRequestTimeMap = new ConcurrentHashMap<>();

    @ApiOperation(value = "生产退料单列表")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Map<String,Object> index(@RequestBody PrdReturnRequest prdReturnRequest) {
        Page<PrdReturn> page = new Page<PrdReturn>(prdReturnRequest.getPage(), prdReturnRequest.getLimit());
        IPage<PrdReturn> list = prdReturnService.selectList(prdReturnRequest, page);
        Map<String,Object> retMap=new HashMap<>();
        retMap.put("data", CommonPage.restPage(list));
        return AjaxMap.success(retMap);
    }
    /**
     * @explain 查看对象明细
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询prdfeed明细")
    @ResponseBody
    @PostMapping(value = "/prdreturndetail")

    public AjaxResult<Map<String,Object>> getPrdReturnDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = prdReturnService.getPrdReturnDetail(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }

    /**
     * @explain 获取工序计划单中领料和补料信息
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "获取工序计划单中领料和补料信息")
    @ResponseBody
    @PostMapping(value = "/get_prd_return_list")

    public AjaxResult<Map<String,Object>> getPrdReturnList(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{

        Map<String,Object> data = prdReturnService.getPrdReturnList(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }


    /**
     * @explain 添加退料详情(临时数据)
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "获取工序计划单中领料和补料信息")
    @ResponseBody
    @PostMapping(value = "/add_prd_return_detail")

    public AjaxResult<?> addPrdReturnDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        Boolean res = prdReturnService.addPrdReturnDetail(userId,map);
        if (res){
            return AjaxResult.success(res);
        } else {
            return AjaxResult.failed();
        }
    }


    /**
     * @explain 查询退料单明细(手机端)
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询退料单明细(手机端)")
    @ResponseBody
    @PostMapping(value = "/get_prd_return_temp")

    public AjaxResult<Map<String,Object>> getPrdReturnTemp(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = prdReturnService.getPrdReturnTemp(map,user_id);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * 删除领料中的临时数据
     * @param map
     * @return
     */
    @ApiOperation(value = "删除退料中的临时数据")
    @ResponseBody
    @PostMapping(value = "/delete_return_detail_temp")
    public AjaxResult<?> deleteReturnDetailTemp(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();

        // 处理请求
        Boolean res = prdReturnService.deleteReturnDetailTemp(userName,map);

        return AjaxResult.success(res);

    }


    /**
     * 创建补料单
     * @param map
     * @return
     */
    @ApiOperation(value = "生成退料单")
    @ResponseBody
    @PostMapping(value = "/create_return_bill")
    public AjaxResult<Boolean> createReturnBill(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        //从map中查找上次请求的时间戳
        Long lastRequestTime = lastRequestTimeMap.get(userId);
        //如果为空或者上次请求的时间戳与当前时间做差大于5s，则视为新请求，否则重复请求。
        if (lastRequestTime == null || System.currentTimeMillis() - lastRequestTime > 20000) {
            lastRequestTimeMap.put(userId, System.currentTimeMillis());
            // 处理请求
            Boolean res = prdReturnService.createReturnBill(userName,map);
            if (res){
                return AjaxResult.success(res);
            } else {
                return AjaxResult.failed();
            }
        } else {
            // 重复请求
            throw new Exception("正在创建退料，请不要重复点击");
        }


    }
}
